package com.example.oasystem.controller.mail;

import com.example.oasystem.entity.mail.AoaInMailListOfSun;
import com.example.oasystem.entity.mail.AoaMailnumberOfl;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.type.AoaTypeList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaInMailListServiceOfSun;
import com.example.oasystem.service.AoaMailnumberServiceOfl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AoaMailnumberControllerOfl {
    @Autowired
    AoaMailnumberServiceOfl aoaMailnumberServiceOfl;

    @Autowired
    private AoaInMailListServiceOfSun aoaInMailListServiceOfSun;

    @RequestMapping("/accountmanage")
    public String accountmanage(HttpSession session, Model model,
                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                            @RequestParam(value = "baseKey", defaultValue = "") String baseKey){
        AoaUser user = (AoaUser) session.getAttribute("user");
        Long mailNumUserId = user.getUserId();
        PageInfo<AoaMailnumberOfl> mailNumberPageInfo = aoaMailnumberServiceOfl.mailNumber(mailNumUserId, baseKey, pageNum, pageSize);
        Page page=new Page(mailNumberPageInfo.getSize(),Math.toIntExact(mailNumberPageInfo.getTotal()),mailNumberPageInfo.getPages(),mailNumberPageInfo.getPageNum(),mailNumberPageInfo.isIsFirstPage(),mailNumberPageInfo.isIsLastPage());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("account",mailNumberPageInfo.getList());
        model.addAttribute("url", "mailtable");
        return "/mail/mailmanage";
    }
    @RequestMapping("/mailtable")
    public String mailtable(HttpSession session, Model model,
                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                            @RequestParam(value = "baseKey", defaultValue = "") String baseKey){
        AoaUser user = (AoaUser) session.getAttribute("user");
        Long mailNumUserId = user.getUserId();
        PageInfo<AoaMailnumberOfl> mailNumbersPageInfo = aoaMailnumberServiceOfl.mailNumbers(mailNumUserId,baseKey,null,null,null,pageNum,pageSize);
        Page page=new Page(mailNumbersPageInfo.getSize(),Math.toIntExact(mailNumbersPageInfo.getTotal()),mailNumbersPageInfo.getPages(),mailNumbersPageInfo.getPageNum(),mailNumbersPageInfo.isIsFirstPage(),mailNumbersPageInfo.isIsLastPage());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("account",mailNumbersPageInfo.getList());
        model.addAttribute("url", "mailtable");
        return "/mail/mailtable";
    }
    @RequestMapping("/delete")
    public String delete(@RequestParam("id")Long id, @RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum){
        List<AoaInMailListOfSun> mailLists = aoaMailnumberServiceOfl.mailList(id);
        for (AoaInMailListOfSun mailList : mailLists) {
            int i = aoaMailnumberServiceOfl.delAttachment(mailList.getMailFileId());
            int j = aoaMailnumberServiceOfl.delMailReciver(mailList.getMailId());
            int z = aoaMailnumberServiceOfl.delMailList(mailList.getMailId());
        }
        int i = aoaMailnumberServiceOfl.deleteByPrimaryKey(id);
        return "forward:/accountmanage?pageNum="+pageNum;
    }
    @RequestMapping("/addaccount")
    public String addaccount(Model model,@RequestParam("id")Long id,HttpSession session,HttpServletRequest request,
                             @RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum
                             ){
        //存储状态
        List<AoaStatusList> status = aoaMailnumberServiceOfl.selectStatusListByRealModel("aoa_mailnumber");
        model.addAttribute("statuslist",status);
        //存储类型
        List<AoaTypeList> type = aoaMailnumberServiceOfl.selectTypeListByRealModel("aoa_mailnumber");
        model.addAttribute("typelist",type);

        AoaUser user = (AoaUser) session.getAttribute("user");
        String userName = user.getUserName();
        model.addAttribute("username",userName);
        if (id==null){

        }
        else {

            AoaMailnumberOfl mailnumber= aoaMailnumberServiceOfl.selectByPrimaryKey(id);
            model.addAttribute("mails",mailnumber);
            model.addAttribute("pageNum",pageNum);
        }
        return "/mail/addaccounts";
    }
    @RequestMapping("/saveaccount")
    public String saveaccount(AoaMailnumberOfl mailnumber, HttpServletRequest request,@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum
    ){
        String re="";
        if (mailnumber.getMailNumberId()==null){
            //时间格式化
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String nowdatehms = sdf.format(date);  //获取当前系统时间
            mailnumber.setMailCreateTime(nowdatehms);
            aoaMailnumberServiceOfl.insertSelective(mailnumber);
            re="forward:/accountmanage";
        }
        else {

            aoaMailnumberServiceOfl.updateByPrimaryKeySelective(mailnumber);
            re="forward:/accountmanage?pageNum="+pageNum;
        }
        return re;
    }
}
